﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using canthovieclam.BL;
using canthovieclam.BL.DataContact.Enum;

namespace canthovieclam.Helper
{
    public static class DropDownListHelper
    {
        public static MvcHtmlString SelectEmployerStates<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            var items = new List<SelectListItem>();
            items = Enum.GetValues(typeof(EmployerState)).Cast<EmployerState>().Select(x => new SelectListItem { Value = ((int)x).ToString(), Text = x.ToString() }).ToList();

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString SelectCountries<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            CountryImpl countryImpl = new CountryImpl();
            var items = new List<SelectListItem>();

            items = countryImpl.GetCountryList().Select(x => new SelectListItem { Value = x.CountryId.ToString(), Text = x.CountryName }).ToList();

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString SelectLocations<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            LocationImpl locationImpl = new LocationImpl();
            var items = new List<SelectListItem>();
            items = locationImpl.GetLocationModelList().Select(x => new SelectListItem { Text = x.LocationName, Value = x.LocationId.ToString() }).ToList();

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString SelectEmployerTypes<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            EmployerTypeImpl employerTypeImpl = new EmployerTypeImpl();
            var items = new List<SelectListItem>();
            items = employerTypeImpl.GetEmployerModelList().Select(x => new SelectListItem { Text = x.EmployerTypeName, Value = x.EmployerTypeId.ToString() }).ToList();

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString SelectAllJobCategories<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            JobCatelogyImpl jobCategoryImpl = new JobCatelogyImpl();
            var items = new List<SelectListItem>();
            items = jobCategoryImpl.GetJobCategoryList().Select(x => new SelectListItem { Text = x.JobCategoryName, Value = x.JobCategoryId.ToString() }).ToList();

            return htmlHelper.DropDownListFor(expression, items);
        }

        public static MvcHtmlString RadioGenders<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            JobAttributeImpl jobAttributeImpl = new JobAttributeImpl();
            var items = new List<SelectListItem>();
            items = jobAttributeImpl.GetJobAttributes().Select(x => new SelectListItem { Text = x.JobAttributeName, Value = x.JobAttributeId.ToString() }).ToList();

            return htmlHelper.RenderRadioButtonList(expression, items);
        }

        public static MvcHtmlString RenderRadioButtonList<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> items)
        {
            var metaData = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
            var sb = new System.Text.StringBuilder();

            if (items != null)
            {
                // Create a radio button for each item in the list 
                foreach (SelectListItem item in items)
                {
                    // Generate an id to be given to the radio button field 
                    var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);

                    // Create and populate a radio button using the existing html helpers 
                    var label = helper.Label(id, HttpUtility.HtmlEncode(item.Text));
                    var radio = helper.RadioButtonFor(expression, item.Value, new { id = id }).ToHtmlString();

                    // Create the html string that will be returned to the client 
                    // e.g. <input data-val="true" data-val-required="You must select an option" id="TestRadio_1" name="TestRadio" type="radio" value="1" /><label for="TestRadio_1">Line1</label> 
                    sb.AppendFormat("<div class=\"option\">{0}{1}</div>", radio, label);
                }
            }

            return MvcHtmlString.Create(sb.ToString());
        }
    }
}